package com.hc.conferencecheck.controller;

import com.alibaba.fastjson.JSONObject;
import com.hc.conferencecheck.entity.ResultMsg;
import com.hc.conferencecheck.entity.ResultStatusCode;
import com.hc.conferencecheck.service.ConventionerService;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 与会者controller
 */
@RestController
@RequestMapping("/conventionerController")
public class ConventionerController {
    @Resource
    private ConventionerService conventionerServiceImpl;

    /**
     * 根据手机号查询用户
     */
    /**
     * 根据手机号查询用户
     */
    @RequestMapping("/selectConventionerByPhoneNumber")
    public ResultMsg selectConventionerByPhoneNumber(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        Object phoneNumberObj = params.get("phoneNumber");
        if (phoneNumberObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "用户手机号不可为空");
        }
        ResultMsg resultMsg = conventionerServiceImpl.selectConventionerByPhoneNumber(params);

        return resultMsg;
    }


    /**
     * 删除用户信息
     */
    @RequestMapping("/deleteConventioner")
    public ResultMsg deleteConventioner(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        Object conventionerIdObj = params.get("conventionerId");
        if (conventionerIdObj == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "用户ID不可为空");
        }
        ResultMsg resultMsg = conventionerServiceImpl.deleteConventioner(params);

        return resultMsg;
    }

    /**
     * 查询用户基础信息
     */
    @RequestMapping("/selectConventionerBaseInfo")
    public ResultMsg selectConventionerBaseInfo(String param) {
        Map params = (Map) JSONObject.parseObject(param);
        Object pageNumber = params.get("pageNumber");
        if (pageNumber == null) {
            return new ResultMsg(ResultStatusCode.EORROR.getCode(), ResultStatusCode.EORROR.getMsg(), "当前页数参数不可为空");
        }
        ResultMsg resultMsg = conventionerServiceImpl.selectConventionerBaseInfo(params);

        return resultMsg;
    }

    /**
     * 删除用户
     */

    /**
     * 用户登出接口
     */
    @RequestMapping("/logout")
    public ResultMsg logout(HttpServletRequest request, HttpServletResponse response) {
        //清空session
        HttpSession session = request.getSession();
        session.invalidate();
        return new ResultMsg(ResultStatusCode.OK.getCode(), ResultStatusCode.OK.getMsg(), "用户登出成功");
    }

    /**
     * 查询所有已注册用户
     */
    @RequestMapping("/selectAllConventioners")
    public ResultMsg selectAllConventioners(@RequestBody Map<String, Object> params) {
        ResultMsg resultMsg = conventionerServiceImpl.selectAllConventioners(params);
        return resultMsg;
    }

    /**
     * 修改密码接口
     */
    @RequestMapping("/editPassWord")
    public ResultMsg editPassWord(String param, HttpServletRequest request) {
        Map params = (Map) JSONObject.parseObject(param);
        ResultMsg resultMsg = conventionerServiceImpl.editPassWord(params, request);
        return resultMsg;
    }
    /**
     * 与会者登录接口
     */
    @RequestMapping("/login")
    public ResultMsg login(String param, HttpServletRequest request, HttpServletResponse response) {
//        Map params = (Map) JSONObject.parseObject(param);
//        ResultMsg resultMsg = conventionerServiceImpl.login(params, request, response);
//
//        String callback = request.getParameter("callback");
//        MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(JSONObject.toJSON(resultMsg).toString());
//        mappingJacksonValue.setJsonpFunction(callback);
//        return mappingJacksonValue;
        Map params = (Map) JSONObject.parseObject(param);
        ResultMsg resultMsg = conventionerServiceImpl.login(params, request, response);
        return resultMsg;
    }

    /**
     * 与会者登录接口
     */

    @RequestMapping("/login02")
    public Object login(String param, HttpServletRequest request, HttpServletResponse response, String callback) {
        Map params = (Map) JSONObject.parseObject(param);
        ResultMsg resultMsg = conventionerServiceImpl.login(params, request, response);

        String callback2 = request.getParameter("callback");
        MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(JSONObject.toJSON(resultMsg).toString());
        mappingJacksonValue.setJsonpFunction(callback2);
        return mappingJacksonValue;
    }

    /**
     * 与会者注册接口
     */
    @RequestMapping("/register")
    public ResultMsg register(String param, HttpServletRequest request) {
        Map params = (Map) JSONObject.parseObject(param);

        ResultMsg resultMsg = conventionerServiceImpl.register(params, request);

        return resultMsg;
    }

    /**
     * 校验手机号，手机号是否符合格式，手机号是否已经注册
     */
    @RequestMapping("/matchPhoneNumber")
    public ResultMsg matchPhoneNumber(String param, HttpServletRequest request) {
        Map params = (Map) JSONObject.parseObject(param);

        ResultMsg resultMsg = conventionerServiceImpl.matchPhoneNumber(params);

        return resultMsg;
    }
}
